package com.hjl.mybatis.servlet; /**
 * @author ServivE
 * @className ${NAME}
 * @description ${description}
 * @version 0.1
 * @date 2024/8/2 10:58
 * @since jdk11
 */

import com.hjl.mybatis.entity.Emp;
import com.hjl.mybatis.mapper.EmpMapper;
import com.hjl.mybatis.utils.SqlSessionFactoryUtil;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "AddEmpServlet", value = "/addEmp")
public class AddEmpServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String ename = request.getParameter("ename");
        String esex = request.getParameter("esex");
        Double sal = Double.parseDouble(request.getParameter("sal"));

        Emp emp = new Emp(ename, esex, sal);

        SqlSession sqlSession = SqlSessionFactoryUtil.getSqlSession();
        EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
        int i = empMapper.insertEmp(emp);

        if (i > 0) {
            response.sendRedirect("empInfo.html");
        }else{
            response.sendRedirect("addEmp.html");
        }


    }
}
